
// index.js
const app = getApp();
const mapObj = require('../../libs/amap-wx');
const HTTP = require('../../utils/util');
const common = require('../../libs/common');
Page({
    data: {
        location: {
            longitude:'',
            latitude:''
        },
        swiperList: [],
        shopCateList: [],
        shopCateList2: [],
        shopList: [],
        city: '',
        shopPage: 1,
        shopPageNumber: 10,
        vip_img: '',
        dotsNumber: [1, 2],
        dotsActive: true,
        searchValue: '',
        loading:false
    },
    onLoad() {
        this.setData({
            swiperList:wx.getStorageSync('swiperList'),
            vip_image:wx.getStorageSync('vip_image'),
            shopCateList:wx.getStorageSync('shopCateList'),
            shopCateList2:wx.getStorageSync('shopCateList2')
        });
        let location = wx.getStorageSync('location');
        if(location){
            this.setData({
                location:location
            });
            this.postLocation();
        }else{
            this.getLocation();
        };
    },
    //用户是否授权
    isAllow(){
        wx.getSetting({
            success:res=>{
                console.log(res);
                if(res.authSetting['scope.userLocation']){
                    this.getLocation();
                }else{
                    wx.showModal({
                        title:"'518联商惠平台'需要获取你的地理位置",
                        content:'你的地理位置将用于展示附近商户',
                        confirmColor:'#00D07F',
                        success:res=>{
                            if (res.confirm) {
                                wx.openSetting({
                                    success:res=>{
                                        console.log('授权成功')
                                        console.log(res)
                                        this.getLocation();
                                    },
                                    fail:res=>{
                                        wx.showToast({
                                            icon:'none',
                                            title:'授权失败！'
                                        });
                                    }
                                });
                              } else if (res.cancel) {
                                console.log('用户点击取消')
                                //发送空坐标
                                this.postLocation();
                              }
                        },
                        fail:res=>{
                            wx.showToast({
                                icon:'none',
                                title:'网络故障，稍后重试'
                            })
                        }
                    });
                    this.getLocation();
                }
            },
            fail:res=>{

            }
        })
    },
    //获取用户坐标
    getLocation() {
        const GDmap = new mapObj.AMapWX({ key: app.globalData.gd_key });
        let arr = {};
        GDmap.getRegeo({
            success: res => {
                res.map(item => {
                    Object.assign(arr, item)
                });
                this.setData({
                    location: arr
                });
                wx.setStorageSync('location',arr)
                //发送用户坐标
                this.postLocation();
            },
            fail: res => {
                console.log(res); 
                this.postLocation();                 
            }
        });
    },
    //发送用户坐标
    postLocation(callback) {
        this.setData({
            loading:true
        });
        HTTP.getData('/api/index/index', {
            longitude: this.data.location['longitude'],
            latitude: this.data.location['latitude'],
            limit: this.data.shopPageNumber,
            page: this.data.shopPage
        }, res => {
            this.setData({
                city: res.data.city.replace('市', ''),
                swiperList: res.data.slide,
                vip_img: res.data.vip_image,
                loading:false
            });
            wx.setStorageSync('swiperlist', res.data.slide);
            wx.setStorageSync('vip_img', res.data.vip_image);
            this.handleCateList(res.data.cate);
            common.handlePageChange(this.data.shopPage, res.data.shop.data, this.data.shopList, (e) => {
                this.setData({
                    shopList: e
                });
            },'商家');
        });

    },
    // 处理分类数组
    handleCateList(res) {
        if (res.length <= 10) {
            this.setData({
                shopCateList: res
            });
        } else {
            let arr1 = [];
            let arr2 = [];
            arr1 = res.slice(0, 10);
            arr2 = res.slice(10)
            this.setData({
                shopCateList: arr1,
                shopCateList2: arr2
            });
            wx.setStorageSync('shopCateList', arr1);
            wx.setStorageSync('shopCateList2', arr2);
        }
    },
    //上拉加载
    onReachBottom(e) {
        this.setData({
            shopPage: this.data.shopPage + 1
        })
        this.postLocation();
    },
    //监听catelist变化
    listChange(e) {
        if (e.detail.currentItemId === '1') {
            this.setData({
                dotsActive: true
            })
        } else if (e.detail.currentItemId === '2') {
            this.setData({
                dotsActive: false
            })
        }
    },
    // gosearch 页面
    goSearch() {
        wx.navigateTo({
            url: '../search/search'
        });
    },
    onShow() {
        
    },
    onSearch(e) {
        console.log(e.detail)
    }
})

